<template>
  <div class="app-container">
    <el-table :data="taskList">
      <el-table-column label="序号" width="50" align="center" key="id" prop="id">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="开始节点名称" align="center" prop="nodeName" :show-overflow-tooltip="true" />
      <el-table-column label="结束节点名称" align="center" prop="targetNodeName" :show-overflow-tooltip="true" />
      <el-table-column label="审批人" align="center" prop="approver" :show-overflow-tooltip="true" />
      <el-table-column label="协作类型" align="center" prop="cooperateType" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :options="cooperateType" :value="scope.row.cooperateType" />
        </template>
      </el-table-column>
      <el-table-column label="协作人" align="center" prop="collaborator" :show-overflow-tooltip="true" />
      <el-table-column label="流程状态" align="center" prop="flowStatus" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :options="flow_status" :value="scope.row.flowStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="message" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" :show-overflow-tooltip="true">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务详情" align="center" width="100">
        <template #default="scope">
          <el-button size="small" v-if="scope.row.ext" @click="handle(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="110" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="small" link v-if="scope.row.nodeType === 1 && scope.row.formCustom === 'Y'"
            @click="handleForm(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <component v-if="approve" :is="approve" :businessId="businessId" :taskId="taskId" :infoVo="infoVo"
      @refresh="fetchTaskList"></component>
    <formDialog v-if="showFormDialog" :disabled="true" :formName="'查看'" v-model:visible="showFormDialog"
      :showApproval="false" :taskId="taskId" :type="'1'"></formDialog>
  </div>
</template>

<script setup>
  import {
    ref,
    defineAsyncComponent,
    onMounted,
    shallowRef
  } from 'vue'
  import {
    doneList
  } from '@/api/flow/execute'
  import formDialog from "../../../form/definition/formDialog.vue"
  import {
    parseTime
  } from '@/utils/ruoyi'
  import {
    useRoute
  } from 'vue-router'
  import {
    useDict
  } from '@/utils/dict'

  const cooperateType = ref([{
      label: '审批',
      value: '1'
    },
    {
      label: '转办',
      value: '2'
    },
    {
      label: '委派',
      value: '3'
    },
    {
      label: '会签',
      value: '4'
    },
    {
      label: '票签',
      value: '5'
    },
    {
      label: '加签',
      value: '6'
    },
    {
      label: '减签',
      value: '7'
    }
  ]);
  const {
    flow_status
  } = useDict('flow_status');
  // 路由实例
  const route = useRoute()
  // 响应式变量
  const instanceId = ref('')
  const formPath = ref('')
  const taskList = ref([])
  const taskId = ref('')
  const businessId = ref('')
  const approve = shallowRef(null)
  const infoVo = ref(null)
  const showFormDialog = ref(false)
  // 激活组件时执行（对应Vue2的activated）
  onMounted(() => {
    instanceId.value = route.params.instanceId
    formPath.value = route.query.formPath
    // 解析路由传递的infoVo（可能需要JSON.parse，根据实际传递方式调整）
    infoVo.value = route.query.infoVo ? JSON.parse(route.query.infoVo) : null
    businessId.value = route.query.businessId
    fetchTaskList()
  })

  // 获取任务列表
  const fetchTaskList = () => {
    doneList(instanceId.value).then(response => {
      console.log('donelist', response)
      taskList.value = response.data
    })
  }

  // 处理查看业务详情
  const handle = (row) => {
    // 设置任务ID和业务ID
    taskId.value = row.id
    // 解析ext字段的JSON数据(扩展)
    if (row.ext) {
      try {
        const parsedExt = JSON.parse(row.ext)
        infoVo.value = {
          ...parsedExt
        }
      } catch (e) {
        console.error('Invalid ext JSON format:', row.ext)
      }
    }
    // 获取或设置formPath路径
    const currentFormPath = row.formPath || formPath.value
    approve.value = defineAsyncComponent(() => import('@/views/system/leave/see.vue'));
  }

  // 查看表单详情
  const handleForm = (id) => {
    taskId.value = id
    showFormDialog.value = true
  }
</script>